package com.ioioioio.controller;

import com.ioioioio.pojo.Resume;
import com.ioioioio.pojo.ReturnMsg;
import com.ioioioio.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

/**
 * @Package: com.ioioioio.controller
 * @Author: Dot
 * @Description:
 * @Date: 2020/10/15 11:02
 * @Version: 1.0
 */
@Controller
public class ManagerController {

    @Autowired
    private UserService userService;

    @RequestMapping("/authenticate")
    public String loginCheck(HttpSession session, ModelMap modelMap, @RequestParam("name") String name, @RequestParam("phone") String phone) {
        Resume loginUser = userService.userInfoCheck(name, phone);
        if (null == loginUser) {
            modelMap.put("errMsg", "用户名或密码不对");
            return "forward:/login.jsp";
        } else {
            session.setAttribute("loginUser", loginUser);
            return "redirect:allAccount";
        }
    }

    @RequestMapping("/allAccount")
    public String loadAllAccount(ModelMap modelMap){
        modelMap.addAttribute("users", userService.allAccount());
        return "index";
    }
    @RequestMapping("/add")
    public String addResume(Resume resume){
        if(!StringUtils.isEmpty(resume.getId())){
            userService.editResume(resume);
        }
        return "redirect:allAccount";
    }

    @RequestMapping("/edit")
    public String editResume(Resume resume){
        if(!StringUtils.isEmpty(resume.getId())){
           userService.editResume(resume);
        }
        return "redirect:allAccount";
    }
    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "forward:/login.jsp";
    }

    @RequestMapping("/del")
    public String removeResume(@RequestParam("id") Long userId){
        if(null != userId){
            userService.removeResume(userId);
        }
        return "redirect:allAccount";
    }

}
